﻿package mortal.game.view.signUp.sevenDayLogin.panel
{
    import Message.Public.*;
    import com.gengine.resource.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import flash.events.*;
    import flash.geom.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.common.guide.data.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.signUp.sevenDayLogin.*;
    import mortal.game.view.signUp.sevenDayLogin.data.*;
    import mortal.game.view.signUp.view.*;
    import mortal.mvc.core.*;

    public class SevenDayLogin extends SignUpViewBase
    {
        protected var _downBg:GImageBitmap;
        protected var _rewardListSp:GSprite;
        private var _cellArr:Array;
        protected var _topBg:GImageBitmap;
        protected var _getBtn:GLoadingButton;
        protected var _getBtnLabel:GBitmap;
        protected var _dayNumTitle:GBitmap;
        protected var _btnList:CellsGridSprite;
        protected var _barBg:GBitmap;
        protected var barUp:GBitmap;
        private var _tipDay:SevenDaySelectTipSp;
        private var isLoadFlag:Boolean = false;
        private var _info:Object;
        private var _allRewardArr:Array;
        private var _selectCell:SevenDayOneCell;

        public function SevenDayLogin()
        {
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._topBg = UIFactory.gImageBitmap(ImagesConst.SevenDayTopBg, 0, 0, this);
            this._downBg = UIFactory.gImageBitmap(ImagesConst.SevenDayDownBg, 0, 189, this);
            this._getBtn = UIFactory.gLoadingButton(ImagesConst.LineOffBtn_upSkin, 378, 525, 145, 22, this);
            this._rewardListSp = UIFactory.getUICompoment(GSprite);
            this._rewardListSp.x = 210;
            this._rewardListSp.y = 418;
            this.addChild(this._rewardListSp);
            this.initRewards();
            LoaderHelp.addResCallBack(ResFileConst.sevenDayLogin, this.onShow);
            this.checkNextDayLogin();
            return;
        }// end function

        private function checkNextDayLogin() : void
        {
            var _loc_1:int = 0;
            var _loc_2:int = 0;
            if (SevenDayLoginUtil.hasNextDayIcon())
            {
                _loc_1 = Cache.instance.role.entityInfo.level;
                _loc_2 = GameDefConfig.instance.getSystemOpenLevel(41);
                if (_loc_1 >= _loc_2 && _loc_1 <= _loc_2 + 3)
                {
                    return;
                }
            }
            return;
        }// end function

        private function onShow() : void
        {
            var _loc_1:Object = null;
            this.isLoadFlag = true;
            this._getBtn.label = "";
            this._getBtnLabel = UIFactory.gBitmap(ImagesConst.SevenDayCanGetFlag, 417, 536, this);
            this._dayNumTitle = UIFactory.gBitmap(ImagesConst.SevenDayGift1, 383, 382, this);
            this._barBg = UIFactory.gBitmap(ImagesConst.SevenDayProgressBg, 100, 257, this);
            this.barUp = UIFactory.gBitmap(ImagesConst.SevenDayProgressBar, 102, 257, this);
            this._btnList = UIFactory.getUICompoment(CellsGridSprite);
            this._btnList.colNum = 7;
            this._btnList.vCellWid = 127;
            this._btnList.hCellWid = 100;
            this._btnList.genCellFun = UIFactory.getUICompoment;
            this._btnList.genCells(7, SevenDayOneCell);
            this._btnList.x = 19;
            this._btnList.y = 228;
            this.addChild(this._btnList);
            this._btnList.configEventListener(MouseEvent.CLICK, this.onDaySelectHd);
            this.genTabDay();
            this._getBtn.configEventListener(MouseEvent.CLICK, this.onGetReward);
            if (Cache.instance.sevenDayLogin.rewardInfo)
            {
                _loc_1 = Cache.instance.sevenDayLogin.rewardInfo.giftInfos;
                this.updateRewardStatus(_loc_1);
            }
            this._tipDay = UIFactory.getUICompoment(SevenDaySelectTipSp, 12, 183, this);
            this.onDaySelectHd(null);
            if (SevenDayLoginIcon.hasInstance() && SevenDayLoginUtil.hasSevenDayReward() == false)
            {
                SevenDayLoginIcon.instance.stopEffect();
            }
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            if (this.isLoadFlag == false)
            {
                return;
            }
            this.isLoadFlag = false;
            SevenDayLoginController.selectDay = -1;
            SevenDayLoginUtil.removeArrow(this._getBtn);
            this.disRewards();
            this._rewardListSp.dispose(param1);
            this._getBtnLabel.dispose(param1);
            this._dayNumTitle.dispose(param1);
            this._btnList.dispose(param1);
            this._downBg = null;
            this._rewardListSp = null;
            this._topBg = null;
            this._getBtn = null;
            this._getBtnLabel = null;
            this._dayNumTitle = null;
            this._btnList = null;
            this._barBg = null;
            this.barUp = null;
            this._selectCell = null;
            this._tipDay.dispose();
            this._tipDay = null;
            return;
        }// end function

        private function genTabDay() : void
        {
            var _loc_2:SevenDayOneCell = null;
            var _loc_1:int = 0;
            while (_loc_1 < this._btnList.cellArr.length)
            {
                
                _loc_2 = this._btnList.cellArr[_loc_1];
                if (_loc_2)
                {
                    _loc_2.updateDay((_loc_1 + 1));
                }
                _loc_1++;
            }
            return;
        }// end function

        private function genCellFun() : SevenDayGiftCell
        {
            var _loc_1:* = UIFactory.getUICompoment(SevenDayGiftCell);
            _loc_1.setItemStyle(ItemStyleConst.Big, ImagesConst.ShopItemBg, 6, 6);
            return _loc_1;
        }// end function

        private function genRewardCells(param1:int) : void
        {
            var _loc_2:int = 0;
            var _loc_4:SevenDayGiftCell = null;
            _loc_2 = 0;
            this.disRewards();
            this._cellArr = [];
            var _loc_3:* = (884 - param1 * 72) / 2;
            _loc_2 = 0;
            while (_loc_2 < param1)
            {
                
                _loc_4 = this.genCellFun();
                _loc_4.x = _loc_3 + _loc_2 * 72;
                _loc_4.y = 431;
                this.addChild(_loc_4);
                this._cellArr[_loc_2] = _loc_4;
                _loc_4.isDragAble = false;
                _loc_4.isDropAble = false;
                _loc_4.isThrowAble = false;
                _loc_2++;
            }
            return;
        }// end function

        private function disRewards() : void
        {
            var _loc_2:SevenDayGiftCell = null;
            var _loc_1:int = 0;
            if (this._cellArr)
            {
                _loc_1 = 0;
                while (_loc_1 < this._cellArr.length)
                {
                    
                    _loc_2 = this._cellArr[_loc_1];
                    _loc_2.dispose();
                    _loc_1++;
                }
                this._cellArr = null;
            }
            return;
        }// end function

        public function updateRewardStatus(param1:Object) : void
        {
            var _loc_2:String = null;
            var _loc_3:int = 0;
            var _loc_4:Object = null;
            var _loc_5:SevenDayOneCell = null;
            if (this.isLoadFlag == false)
            {
                return;
            }
            this._info = param1;
            if (this._info == false)
            {
                return;
            }
            for (_loc_2 in param1)
            {
                
                _loc_3 = parseInt(_loc_2);
                _loc_4 = param1[_loc_3];
                _loc_5 = this._btnList.cellArr[(_loc_3 - 1)];
                if (_loc_5)
                {
                    _loc_5.updateStaues(_loc_4);
                }
            }
            return;
        }// end function

        private function onDaySelectHd(event:MouseEvent) : void
        {
            var _loc_3:SevenDayOneCell = null;
            var _loc_5:Array = null;
            var _loc_6:int = 0;
            var _loc_7:SevenDayGiftCell = null;
            var _loc_8:ItemData = null;
            if (this.isLoadFlag == false)
            {
                return;
            }
            var _loc_2:int = 0;
            if (event == null)
            {
                _loc_2 = Cache.instance.sevenDayLogin.getFirstNotGot() - 1;
                _loc_3 = this._btnList.cellArr[_loc_2];
            }
            else
            {
                if (event.target is SevenDayOneCell)
                {
                    _loc_3 = event.target as SevenDayOneCell;
                }
                else
                {
                    _loc_3 = event.target.parent as SevenDayOneCell;
                }
                if (_loc_3)
                {
                    _loc_2 = this._btnList.cellArr.indexOf(_loc_3);
                }
            }
            if (this._selectCell)
            {
                this._selectCell.setSelect(false);
            }
            _loc_3.setSelect(true);
            this._selectCell = _loc_3;
            SevenDayLoginController.selectDay = _loc_2 + 1;
            if (_loc_2 != -1)
            {
                if (this._allRewardArr == null)
                {
                    this._allRewardArr = SevenDayLoginUtil.getShowRewardsByIndex();
                }
                _loc_5 = this._allRewardArr[_loc_2];
                if (_loc_5 == null)
                {
                    return;
                }
                _loc_5 = this.filterItem(_loc_5);
                this.genRewardCells(_loc_5.length);
                _loc_6 = 0;
                while (_loc_6 < _loc_5.length)
                {
                    
                    _loc_7 = this._cellArr[_loc_6];
                    _loc_7.isShowLock = true;
                    if (_loc_7)
                    {
                        _loc_8 = _loc_5[_loc_6];
                        _loc_7.itemData = _loc_8;
                    }
                    _loc_6++;
                }
            }
            var _loc_4:* = _loc_2 + 1;
            this.updateBtn(_loc_4);
            this.updateDayTip(_loc_4);
            this._tipDay.x = 22 + _loc_2 * 127;
            this._tipDay.updateDayNum(_loc_4);
            return;
        }// end function

        private function initRewards() : void
        {
            var _loc_2:SevenDayOneCell = null;
            var _loc_3:Array = null;
            var _loc_4:int = 0;
            var _loc_5:SevenDayGiftCell = null;
            var _loc_6:ItemData = null;
            var _loc_1:int = 0;
            _loc_1 = Cache.instance.sevenDayLogin.getFirstNotGot() - 1;
            if (_loc_1 != -1)
            {
                if (this._allRewardArr == null)
                {
                    this._allRewardArr = SevenDayLoginUtil.getShowRewardsByIndex();
                }
                _loc_3 = this._allRewardArr[_loc_1];
                if (_loc_3 == null)
                {
                    return;
                }
                _loc_3 = this.filterItem(_loc_3);
                this.genRewardCells(_loc_3.length);
                _loc_4 = 0;
                while (_loc_4 < _loc_3.length)
                {
                    
                    _loc_5 = this._cellArr[_loc_4];
                    _loc_5.isShowLock = true;
                    if (_loc_5)
                    {
                        _loc_6 = _loc_3[_loc_4];
                        _loc_5.itemData = _loc_6;
                    }
                    _loc_4++;
                }
            }
            return;
        }// end function

        private function filterItem(param1:Array) : Array
        {
            var _loc_4:SPlayerItem = null;
            var _loc_5:ItemData = null;
            var _loc_6:int = 0;
            var _loc_2:Array = [];
            var _loc_3:int = 0;
            while (_loc_3 < param1.length)
            {
                
                _loc_4 = param1[_loc_3];
                _loc_5 = new ItemData(_loc_4);
                if (_loc_5.itemInfo)
                {
                    _loc_6 = Cache.instance.role.entityInfo.career;
                    if (ItemsUtil.isFitCarrer(_loc_5))
                    {
                        _loc_2.push(_loc_5);
                    }
                }
                else
                {
                    _loc_2.push(_loc_5);
                }
                _loc_3++;
            }
            return _loc_2;
        }// end function

        private function updateDayTip(param1:int) : void
        {
            this._dayNumTitle.bitmapData = GlobalClass.getBitmapData(ImagesConst["SevenDayGift" + param1]);
            return;
        }// end function

        private function updateBtn(param1:int) : void
        {
            var _loc_2:* = Cache.instance.sevenDayLogin.rewardInfo.giftInfos[param1];
            this.showGuideArrow(ESevenDayStatus.canGet == _loc_2);
            if (ESevenDayStatus.canGet == _loc_2)
            {
                this._getBtn.enabled = true;
                this._getBtn.filters = null;
            }
            else
            {
                this._getBtn.enabled = false;
                this._getBtn.filters = [FilterConst.colorFilter2];
            }
            return;
        }// end function

        private function showGuideArrow(param1:Boolean) : void
        {
            if (param1)
            {
                SevenDayLoginUtil.genGiftArrow(this._getBtn, -10, 21, 2, this);
            }
            else
            {
                SevenDayLoginUtil.removeArrow(this._getBtn);
            }
            return;
        }// end function

        private function onGetReward(event:MouseEvent) : void
        {
            if (this._selectCell == null)
            {
                return;
            }
            var _loc_2:* = this._btnList.cellArr.indexOf(this._selectCell);
            Dispatcher.dispatchEvent(new DataEvent(EventName.SevenDayLoginGotReward, _loc_2));
            if (AutoGuideCache.isGuidingSevenDay)
            {
                GameController.signUp._sevenDayController.view.hide();
                AutoGuideCache.isGuidingSevenDay = false;
            }
            return;
        }// end function

        public function updateBtnByDay(param1:int, param2:Object) : void
        {
            var _loc_3:* = this._btnList.cellArr[(param1 - 1)];
            if (_loc_3 && param2)
            {
                _loc_3.updateStaues(param2);
            }
            this.updateBtn(param1);
            return;
        }// end function

        public function flyIcons() : void
        {
            var _loc_1:int = 0;
            var _loc_2:SevenDayGiftCell = null;
            var _loc_3:Point = null;
            if (this._cellArr)
            {
                _loc_1 = 0;
                while (_loc_1 < this._cellArr.length)
                {
                    
                    _loc_2 = this._cellArr[_loc_1];
                    _loc_3 = _loc_2.localToGlobal(GameConst.yellowPoint);
                    FlyToNavbarTool.flyBitmapOriginalToPack(_loc_2.bitmapdata, _loc_3);
                    _loc_1++;
                }
            }
            return;
        }// end function

    }
}
